package cn.bass.marketing.common.vo.resp;

import cn.bass.marketing.common.constant.RulesConstant;
import cn.bass.marketing.common.constant.StringSignConstant;
import cn.bass.marketing.common.exception.AppException;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 策略规则
 */
@Data
@ApiModel(value = "奖品策略")
public class MarketingStrategyRuleRespVO implements Serializable {
    /**
     * 主键
     */
    @JsonProperty("cId")
    @ApiModelProperty(value = "主键")
    private Integer cId;

    /**
     * 策略ID
     */
    @JsonProperty("cStrategyId")
    @ApiModelProperty(value = "策略ID")
    private Integer cStrategyId;

    /**
     * 奖品ID
     */
    @JsonProperty("cPrizeId")
    @ApiModelProperty(value = "奖品ID")
    private Integer cPrizeId;

    /**
     * 规则类型
     */
    @JsonProperty("cRuleType")
    @ApiModelProperty(value = "规则类型")
    private String cRuleType;


    /**
     * 规则模型
     */
    @JsonProperty("cRuleModels")
    @ApiModelProperty(value = "规则模型")
    private String cRuleModels;


    /**
     * 规则内容
     */
    @JsonProperty("cRuleValue")
    @ApiModelProperty(value = "规则内容")
    private String cRuleValue;

    /**
     * 规则描述
     */
    @JsonProperty("cRuleDesc")
    @ApiModelProperty(value = "规则描述")
    private String cRuleDesc;

    /**
     * 状态(ENABLED-启用, STOP-停用, DELETE-删除)
     */
    @JsonProperty("cStatus")
    @ApiModelProperty(value = "状态(ENABLED-启用, STOP-停用, DELETE-删除)")
    private String cStatus;


    public Map<String, List<Integer>> getRuleModels() {
        if (!RulesConstant.RULE_WEIGHT.getValue().equals(cRuleModels)) {
            return Collections.emptyMap();
        }
        String[] ruleValueGroups = cRuleValue.split(StringSignConstant.EMPTY);
        if (ruleValueGroups.length == 0) {
            return Collections.emptyMap();
        }
        Map<String, List<Integer>> ruleModelMaps = Maps.newHashMap();
        for (String ruleValue : ruleValueGroups) {
            if (ruleValue.isEmpty()) {
                continue;
            }
            String[] params = ruleValue.split(StringSignConstant.COLON);
            if (params.length != 2) {
                throw new AppException("规则模型格式错误：" + cRuleValue);
            }
            ruleModelMaps.put(ruleValue, Arrays.stream(params[1].split(StringSignConstant.COMMA)).map(Integer::parseInt).collect(Collectors.toList()));
        }
        return ruleModelMaps;
    }

}